import {createStore} from 'vuex'

export default createStore({
    state() {
        return {
            count: 1,
            users: [
                {id: 1, name: "马化腾", age: 22},
                {id: 2, name: "马云", age: 28},
            ]
        }
    },
    getters: {
        doubleCount(state, getters) {
            return state.count * 10
        },
        totalAge(state, getters) {
            return state.users.reduce((previousValue, currentValue) => previousValue += currentValue.age,0)
        },
        getUserById(state, getters) {
            return (id) => {
                return state.users.find(item => item.id === id)
            }
        },
        getDoubleTotalAge(state,getters){
            return getters.totalAge*10
        }
    },
    mutations: {
        changeCountMutation(state, count) {
            state.count = count
        }
    },
    actions: {
        changeCountAction(context, count) {
            context.commit("changeCountMutation", count += 1)
        }
    },
    modules: {}
})
